﻿using System;
using System.Linq;
using System.Net;
using System.Runtime.Remoting;
using Imgur.Net.Auth;
using NUnit.Framework;
using TestableHttpWebResponse;

namespace Imgur.Net.Test.ImageProvider
{
    [TestFixture]
    public class GetAlbumInfoTests
    {

        private const string albumId = "PEVrZ";

        private const string clientId = "b56c4ebcd84a2c5";
        private const string clientSecret = "5b83ef6e0100a5a8544cc03f2e49c470d7f7cb2f";

        private const string baseTestUriTemplate = "test://api.imgur.com/3/album";

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            WebRequest.RegisterPrefix("test", TestableWebRequestCreateFactory.GetFactory());
        }

        [Test]
        public void TestGetBasicAlbumInfo()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            
            AlbumProvider albumProvider = new DefaultAlbumProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(string.Format(baseTestUriTemplate + "/{0}", albumId)));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success", ResourceLoader.GetResourceText("Resources/AlbumProvider/AlbumModel.basic.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);
           
            AlbumInfo info = albumProvider.GetAlbumInfo("PEVrZ");

            Assert.IsNotNull(info);

            Assert.IsNull(info.AccountUrl);
            Assert.AreEqual("hdpr22d", info.AlbumCoverId);
            Assert.AreEqual(1600, info.AlbumCoverHeight);
            Assert.AreEqual(2560, info.AlbumCoverWidth);
            Assert.AreEqual(new DateTime(2014, 11, 01, 14, 10, 10), info.TimeStamp);
            Assert.IsNull(info.Description);
            Assert.IsNotNull(info.IsFavorite);
            Assert.IsFalse(info.IsFavorite.Value);
            Assert.AreEqual("PEVrZ", info.Id);
            
            Assert.AreEqual(LayoutStyle.Blog, info.Layout);
            Assert.AreEqual("http://imgur.com/a/PEVrZ", info.Link);
            Assert.IsNull(info.IsNSFW);
            Assert.AreEqual(PrivacyLevel.Public, info.PrivacyLevel);
            Assert.IsNull(info.Section);
            Assert.AreEqual("AlbumTitleHere", info.Title);
            Assert.AreEqual(0, info.Views);

            Assert.AreEqual(info.ImagesCount, info.Images.Count);
            Assert.AreEqual(4, info.ImagesCount);
            Assert.IsNotNull( info.Images );
            Assert.AreEqual(4, info.Images.Count);
        }

        [Test]
        public void TestGetFullAlbumInfo()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            
            AlbumProvider albumProvider = new DefaultAlbumProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(string.Format(baseTestUriTemplate + "/{0}", albumId)));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success", ResourceLoader.GetResourceText("Resources/AlbumProvider/AlbumModel.full.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);
           
            AlbumInfo info = albumProvider.GetAlbumInfo("PEVrZ");

            Assert.IsNotNull(info);

            Assert.AreEqual("http://imgur.com/users/12345",info.AccountUrl);
            Assert.AreEqual("hdpr22d", info.AlbumCoverId);
            Assert.AreEqual(1600, info.AlbumCoverHeight);
            Assert.AreEqual(2560, info.AlbumCoverWidth);
            Assert.AreEqual(new DateTime(2014, 11, 01, 14, 10, 10), info.TimeStamp);
            Assert.AreEqual("Album Description",info.Description);
            Assert.IsNotNull(info.IsFavorite);
            Assert.IsTrue(info.IsFavorite.Value);
            Assert.AreEqual("PEVrZ", info.Id);
            
            Assert.AreEqual(LayoutStyle.Blog, info.Layout);
            Assert.AreEqual("http://imgur.com/a/PEVrZ", info.Link);
            Assert.IsNotNull(info.IsNSFW);
            Assert.IsTrue(info.IsNSFW.Value);
            Assert.AreEqual(PrivacyLevel.Public, info.PrivacyLevel);
            Assert.AreEqual("cats",info.Section);
            Assert.AreEqual("AlbumTitleHere", info.Title);
            Assert.AreEqual(16, info.Views);
            Assert.AreEqual(5, info.Order);
            Assert.AreEqual("abcdefgh", info.DeleteHash);

            Assert.AreEqual(info.ImagesCount, info.Images.Count);
            Assert.AreEqual(4, info.ImagesCount);
            Assert.IsNotNull( info.Images );
            Assert.AreEqual(4, info.Images.Count);
        }

        [Test]
        public void TestNoImagesGetAlbumInfo()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);

            AlbumProvider albumProvider = new DefaultAlbumProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(string.Format(baseTestUriTemplate + "/{0}", albumId)));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success", ResourceLoader.GetResourceText("Resources/AlbumProvider/AlbumModel.noimages.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            AlbumInfo info = albumProvider.GetAlbumInfo("PEVrZ");

            Assert.IsNotNull(info);

            Assert.AreEqual("PEVrZ", info.Id);

            Assert.AreEqual(info.ImagesCount, info.Images.Count);
            Assert.AreEqual(0, info.ImagesCount);
            Assert.IsNotNull(info.Images);
            Assert.AreEqual(0, info.Images.Count);
        }

        [Test]
        public void TestSingleImageGetAlbumInfo()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);

            AlbumProvider albumProvider = new DefaultAlbumProvider(authProvider, baseTestUriTemplate);

            var expectedRequest = new TestableWebRequest(new Uri(string.Format(baseTestUriTemplate + "/{0}", albumId)));
            expectedRequest.EnqueueResponse(HttpStatusCode.OK, "Success", ResourceLoader.GetResourceText("Resources/AlbumProvider/AlbumModel.oneimage.json"), false);
            TestableWebRequestCreateFactory.GetFactory().AddRequest(expectedRequest);

            AlbumInfo info = albumProvider.GetAlbumInfo("PEVrZ");

            Assert.IsNotNull(info);

            Assert.AreEqual("PEVrZ", info.Id);

            Assert.AreEqual(info.ImagesCount, info.Images.Count);
            Assert.AreEqual(1, info.ImagesCount);
            Assert.IsNotNull(info.Images);
            Assert.AreEqual(1, info.Images.Count);
        }

        [Test]
        [ExpectedException(typeof (ArgumentNullException))]
        public void TestGetAlbumInfoNullId()
        {
            ImgurAuthProvider authProvider = new AnonymousAuthenticationProvider(clientId);
            AlbumProvider albumProvider = new DefaultAlbumProvider(authProvider, baseTestUriTemplate);
            
            albumProvider.GetAlbumInfo(null);
        }
    }
}
